package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.PageResult2;
import com.itheima.common.Result1;
import com.itheima.common.Result1_1;
import com.itheima.entity.Roles;
import com.itheima.entity.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private HttpServletRequest request;

    @Override
    public Result1 login(String username, String password) {

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getEmail, username);
        User one = this.getOne(wrapper);

        String password1 = one.getPassword();
        Result1 result = new Result1();
        if (password.equals(password1)) {
            UUID uuid = UUID.randomUUID();
            String s = uuid.toString();
            result.setToken(s);
            return result;

        }
        result.setCode(20001);
        result.setError("用户登录失败,用户名或密码错误");
        return result;
    }

    @Override
    public Result1_1 profile() {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getEmail, username);
        User user = this.getOne(wrapper);
        Result1_1 result = new Result1_1();
        result.setAvatar(user.getAvatar());
        result.setIntroduction(user.getIntroduction());
        result.setName(user.getUsername());
        result.setRole(new String[]{user.getRole()});
        result.setRoles(new Roles());
        return result;
    }

    @Override
    public Result1 UpdatePassword(Map<String, String> map) {
        String oldPassword = map.get("oldPassword");
        String newPassword = map.get("newPassword");
        String oldPassword1 = DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        String newPassword1 = DigestUtils.md5DigestAsHex(newPassword.getBytes());

        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getEmail, username);
        User user = this.getOne(wrapper);
        if (user.getPassword().equals(oldPassword1)) {
            user.setPassword(newPassword1);
            this.updateById(user);
        }
        return new Result1();
    }

    @Override
    public Result1 register(User user) {
        this.save(user);
        return new Result1();
    }

    @Override
    public PageResult2 findUserPage(Integer page, Integer pagesize, String username) {

        IPage<User> page1 = new Page<>(page, pagesize, true);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(username != null, User::getUsername, username);
        IPage<User> page2 = this.page(page1, wrapper);
        List<User> records = page2.getRecords();

        //todo 这里total，pages没值
        int total = (int) page2.getTotal();
        int pages = (int) page2.getPages();

        return new PageResult2(total, page, pages, pagesize, records);
    }

    @Override
    public User findById(Integer id) {
        User byId = this.getById(id);
        byId.setPassword(null);
        return byId;
    }

    @Override
    public String add(User user) {
        if (user.getPassword() == null) {
            String s = DigestUtils.md5DigestAsHex((123456 + "").getBytes());
            user.setPassword(s);
        }
        user.setSex("男");
        this.save(user);
        return "添加成功";
    }

    @Override
    public User updateUserById(Integer id, User user) {

        this.updateById(user);
        return null;
    }

    @Override
    public void deleteById(Integer id) {
        boolean b = this.removeById(id);
    }

    @Override
    public void updateState(Integer id, Map<String, String> map) {
        String s = map.get("disabled");
        User byId = this.getById(id);
        if (s.equals("true")) {
            byId.setStatus(1);
            this.updateById(byId);
        }
    }

    @Override
    public List simple() {


        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getEmail, username);
        User user = this.getOne(wrapper);
        ArrayList<User> list = new ArrayList<>();
        list.add(user);
        return list;
    }

    @Override
    public void updatePass(Integer id, Map<String, String> map) {
        String password = map.get("password");
        String s = DigestUtils.md5DigestAsHex(password.getBytes());

        User byId = this.getById(id);
        byId.setPassword(s);
        this.updateById(byId);
    }
}
